home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 421_01 / timer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-24  |  1.3 KB  |  84 lines

  1. // TIMER_C //////////////////////////////////////////////////////////////////
  2.  
  3. // Methods implementing a high speed timer
  4.  
  5. // INCLUDES /////////////////////////////////////////////////////////////////
  6.  
  7. #include <dos.h>
  8. #include "timer.h"
  9.  
  10. // CONSTRUCTOR
  11.  
  12. timer_C::timer_C()
  13. {
  14.   outportb(0x43,0x34);
  15.   outportb(0x40,0x00);
  16.   outportb(0x40,0x00);
  17. }
  18.  
  19. // DESTRUCTOR
  20.  
  21. timer_C::~timer_C()
  22. {
  23.   outportb(0x43,0x36);
  24.   outportb(0x40,0x00);
  25.   outportb(0x40,0x00);
  26. }
  27.  
  28. // READTIMER
  29.  
  30. // Reads the timer with millisecond precision
  31.  
  32. long timer_C::readtimer(void)
  33. {
  34.   asm push si
  35.   asm push di
  36.  
  37.   asm cli
  38.   asm mov dx,0x0020
  39.   asm mov al,0x0a
  40.   asm out dx,al
  41.   asm xor al,al
  42.   asm out 0x43,al
  43.   asm in al,dx
  44.   asm mov di,ax
  45.   asm in  al,0x40
  46.   asm mov bl,al
  47.   asm in  al,0x40
  48.   asm mov bh,al
  49.   asm not bx
  50.   asm in  al,0x21
  51.   asm mov si,ax
  52.  
  53.   asm mov al,0xff
  54.   asm out 0x21,al
  55.   asm mov ax,0x0040
  56.   asm mov es,ax
  57.   asm mov dx,es:[0x006c]
  58.  
  59.   asm mov ax,si
  60.   asm out 0x21,al
  61.  
  62.   asm sti
  63.   asm mov ax,di
  64.   asm test al,0x01
  65.   asm jz done
  66.   asm cmp bx,0x00ff
  67.   asm ja done
  68.   asm inc dx
  69. done:
  70.   asm mov ax,bx
  71.   asm pop di
  72.   asm pop si
  73. }
  74.  
  75. // ELAPSED
  76.  
  77. // Computes the elapsed time, in milliseconds, between two events.
  78.  
  79. long timer_C::elapsed(unsigned long start,unsigned long stop)
  80. {
  81.   return (stop-start)/(long)1193;
  82. }
  83.  
  84.